import {axiosClient} from "@/utils/axiosClient.js";
import {handleCommonParams} from "@/api/commonParams.js";

const requestUrl = '/person/groupFormula'

const api = {

    /**
     * 新增群组公式
     * @param groupFormula
     * @returns {Promise<*|undefined>}
     */
    createGroupFormula: (groupFormula) =>
      axiosClient().post(`${requestUrl}/addInfo`, groupFormula),

    /**
     * 根据ID删除群组公式
     * @param id
     * @returns {Promise<*|undefined>}
     */
    deleteGroupFormulaById: (id) =>
        axiosClient().remove(`${requestUrl}/removeInfo/${id}`),

    /**
     * 根据ID全量更新群组公式
     * @param id
     * @param groupFormula
     * @returns {Promise<*|undefined>}
     */
    updateGroupFormulaFull: (id, groupFormula) =>
        axiosClient().put(`${requestUrl}/modifyInfo/${id}`, groupFormula),

    /**
     * 根据ID部分更新群组公式
     * @param id
     * @param updatePartial
     * @returns {Promise<*|undefined>}
     */
    updateGroupFormulaPartial: (id, updatePartial) =>
        axiosClient().patch(`${requestUrl}/modifyPartialInfo/${id}`, updatePartial),

    /**
     * 根据ID查询群组公式
     * @param id
     * @returns {Promise<*|undefined>}
     */
    getGroupFormulaById: (id) =>
        axiosClient().get(`${requestUrl}/getInfo/${id}`),

    /**
     * 分页查询群组公式
     * @param page
     * @param size
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getGroupFormulaPage: (page, size, businessParam) => {
        const params = new URLSearchParams();
        params.append('page', page)
        params.append('size', size)
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfos?${params.toString()}`)
    },

    /**
     * 根据条件查询群组公式
     * @param businessParam
     * @returns {Promise<axios.AxiosResponse<any>|undefined>}
     */
    getGroupFormulaList: (businessParam) => {
        const params = new URLSearchParams();
        setBusinessParams(params, businessParam)
        return axiosClient().get(`${requestUrl}/getInfoList?${params.toString()}`)
    }

}

const setBusinessParams = (params, businessParam) => {
    if (businessParam) {
        if (businessParam.groupId) {
            params.append('groupId', businessParam.groupId)
        }
        if (businessParam.filterType) {
            params.append('filterType', businessParam.filterType)
        }
        if (businessParam.filterBound) {
            params.append('filterBound', businessParam.filterBound)
        }
        if (businessParam.filterSql) {
            params.append('filterSql', businessParam.filterSql)
        }
        if (businessParam.draftOrgId) {
            params.append('draftOrgId', businessParam.draftOrgId)
        }
        handleCommonParams(params, businessParam);
    }
}

export default api
